﻿using Common.Communication;
using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.View.Base;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;

namespace Common.Device.ViewModels
{
    public class DeviceSetViewModel : BindableBase
    {
        private ObservableCollection<UseDevice> useDevices = new ObservableCollection<UseDevice>(DeviceService.useDevices.Values);
        /// <summary>
        /// 使用设备集合
        /// </summary>
        public ObservableCollection<UseDevice> UseDevices
        {
            get { return useDevices; }
            set { SetProperty(ref useDevices, value); }
        }
        private List<string> deviceTemps = DeviceService.allDevices.GroupBy(o => o.temp).Select(o => o.Key.ToString()).ToList();
        /// <summary>
        /// 设备集合
        /// </summary>
        public List<string> DeviceTemps
        {
            get { return deviceTemps; }
            set { SetProperty(ref deviceTemps, value); }
        }
        private string temp ;
        /// <summary>
        /// 设备
        /// </summary>
        public string Temp
        {
            get { return temp; }
            set
            {
                SetProperty(ref temp, value);
                DeviceTypes = DeviceService.allDevices.Where(o => o.temp.ToString() == value).GroupBy(o => o.type).Select(o => o.Key.ToString()).ToList();
            }
        }
        private List<string> deviceTypes = new List<string>();
        /// <summary>
        /// 设备类型集合
        /// </summary>
        public List<string> DeviceTypes
        {
            get { return deviceTypes; }
            set { SetProperty(ref deviceTypes, value); }
        }
        private string type;
        /// <summary>
        /// 设备类型
        /// </summary>
        public string Type
        {
            get { return type; }
            set
            {
                SetProperty(ref type, value);
                DeviceModels = DeviceService.allDevices.Where(o => o.type.ToString() == value).GroupBy(o => o.model).Select(o => o.Key.ToString()).ToList();
            }
        }
        private List<string> deviceModels = new List<string>();
        /// <summary>
        /// 设备型号集合
        /// </summary>
        public List<string> DeviceModels
        {
            get { return deviceModels; }
            set { SetProperty(ref deviceModels, value); }
        }
        private string model;
        /// <summary>
        /// 设备型号
        /// </summary>
        public string Model
        {
            get { return model; }
            set
            {
                SetProperty(ref model, value);
                DeviceModes = DeviceService.allDevices.FirstOrDefault(o => o.model.ToString() == value)?.mode.Select(o=>o.ToString()).ToList();
                Descripe = DeviceService.allDevices.FirstOrDefault(o => o.model.ToString() == value)?.descripe;
                Parameter2Info = "";
                if (Enum.TryParse(model, true, out DeviceEnum.DeviceModelEnum modelEnum))
                {
                    switch (modelEnum)
                    {
                        case DeviceEnum.DeviceModelEnum.FctModBus:
                            Parameter2Info = "继电器数量;电平采集数量"; break;//参数2输入提示
                        default:
                            break;
                    }
                }
            }
        }
        private string descripe;
        /// <summary>
        /// 设备描述
        /// </summary>
        public string Descripe
        {
            get { return descripe; }
            set { SetProperty(ref descripe, value); }
        }
        private List<string> deviceModes = new List<string>();
        /// <summary>
        /// 设备型号集合
        /// </summary>
        public List<string> DeviceModes
        {
            get { return deviceModes; }
            set { SetProperty(ref deviceModes, value); }
        }
        private bool hasAddr;
        /// <summary>
        /// 是否有地址
        /// </summary>
        public bool HasAddr
        {
            get { return hasAddr; }
            set { SetProperty(ref hasAddr, value); }
        }
        private string mode;
        /// <summary>
        /// 设备通信方式
        /// </summary>
        public string Mode
        {
            get { return mode; }
            set
            {
                SetProperty(ref mode, value);
                if (Enum.TryParse(mode, true, out DeviceEnum.DeviceModeEnum modeEnum))
                    //输入提示
                    switch (modeEnum)
                    {
                        case DeviceEnum.DeviceModeEnum.RS232:
                        case DeviceEnum.DeviceModeEnum.RS485:
                            //串口通信方式
                            PortTtle = "串口号";
                            Ports = Communication.SerialPortManager.GetPortNames().ToList();
                            ParameterTitle = "波特率";
                            Parameters = new List<string> { "2400", "4800", "9600", "19200", "38400", "115200" };
                            HasAddr = true;
                            AddrTitle = "通信地址";
                            break;
                        case DeviceEnum.DeviceModeEnum.USB_NI:
                        case DeviceEnum.DeviceModeEnum.USB_DLL:
                            //USB通信方式
                            PortTtle = "通信端口";
                            Ports = new List<string> { "USB" };
                            ParameterTitle = "通信参数";
                            HasAddr = false;
                            break;
                        case DeviceEnum.DeviceModeEnum.USB_CAN:
                            //USB通信方式
                            PortTtle = "通信端口";
                            Ports = new List<string> { "USB" };
                            ParameterTitle = "通信参数";
                            Parameters = new List<string> { };
                            HasAddr = true;
                            AddrTitle = "通信地址";
                            break;
                        case DeviceEnum.DeviceModeEnum.TCP_Client:
                        case DeviceEnum.DeviceModeEnum.TCP_Server:
                            //TCP通信方式
                            PortTtle = "IP地址";
                            Ports = new List<string> { };
                            ParameterTitle = "端口";
                            Parameters = new List<string> { };
                            HasAddr = true;
                            AddrTitle = "通信地址";
                            break;
                        case DeviceEnum.DeviceModeEnum.HTTP_Post:
                        case DeviceEnum.DeviceModeEnum.HTTP_Get:
                            //HTTP通信方式
                            PortTtle = "接口地址";
                            Ports = new List<string> { };
                            ParameterTitle = "接口URL";
                            Parameters = new List<string> { };
                            HasAddr = false;
                            break;
                        default:
                            break;
                    }
            }
        }
        private string portTtle = "通信端口";
        /// <summary>
        /// 输入通信端口
        /// </summary>
        public string PortTtle
        {
            get { return portTtle; }
            set { SetProperty(ref portTtle, value); }
        }
        private List<string> ports = new List<string>();
        /// <summary>
        /// 通信端口集合
        /// </summary>
        public List<string> Ports
        {
            get { return ports; }
            set
            {
                SetProperty(ref ports, value);
            }
        }
        private string port;
        /// <summary>
        /// 通信端口
        /// </summary>
        public string Port
        {
            get { return port; }
            set { SetProperty(ref port, value); }
        }
        private string parameterTitle = "通信参数";
        /// <summary>
        /// 通信参数提示
        /// </summary>
        public string ParameterTitle
        {
            get { return parameterTitle; }
            set { SetProperty(ref parameterTitle, value); }
        }
        private List<string> parameters = new List<string>();
        /// <summary>
        /// 通信参数集合
        /// </summary>
        public List<string> Parameters
        {
            get { return parameters; }
            set
            {
                SetProperty(ref parameters, value);
            }
        }
        private string parameter;
        /// <summary>
        /// 通信参数
        /// </summary>
        public string Parameter
        {
            get { return parameter; }
            set { SetProperty(ref parameter, value); }
        }
        private string parameter2Title = "设备参数";
        /// <summary>
        /// 设备参数提示
        /// </summary>
        public string Parameter2Title
        {
            get { return parameter2Title; }
            set { SetProperty(ref parameter2Title, value); }
        }
        private string parameter2Info = "";
        /// <summary>
        /// 设备参数输入提示
        /// </summary>
        public string Parameter2Info
        {
            get { return parameter2Info; }
            set { SetProperty(ref parameter2Info, value); }
        }
        private string parameter2;
        /// <summary>
        /// 设备参数
        /// </summary>
        public string Parameter2
        {
            get { return parameter2; }
            set { SetProperty(ref parameter2, value); }
        }
        private string addrTitle = "通信地址";
        /// <summary>
        /// 通信地址提示
        /// </summary>
        public string AddrTitle
        {
            get { return addrTitle; }
            set { SetProperty(ref addrTitle, value); }
        }
        private string addr;
        /// <summary>
        /// 通信地址
        /// </summary>
        public string Addr
        {
            get { return addr; }
            set { SetProperty(ref addr, value); }
        }
        private string name;
        /// <summary>
        /// 设备标识
        /// </summary>
        public string Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }
        public ICommand DelCommand { get => new DelegateCommand<UseDevice>(Del); }
        public void Del(UseDevice dev)
        {
            var result = View.Controls.Dialogs.TipView.Show($"确认删除设备[{dev.name}]?", new Dictionary<string, Brush> { { "删除", View.Base.Colors.PrimaryColor }, { "取消", View.Base.Colors.DangerColor } });
            if (result == "删除")
            {
                try
                {
                    DeviceDbHelper.db.Deleteable(dev).ExecuteCommand();
                    UseDevices = new ObservableCollection<UseDevice>(DeviceService.useDevices.Values);
                }
                catch (Exception ex)
                {
                    HandyControl.Controls.MessageBox.Error("删除设备失败" + ex.Message); return;
                }
            }
        }
        public ICommand HnadCommand { get => new DelegateCommand<UseDevice>(Hand); }
        public void Hand(UseDevice dev)
        {
            //调试界面
            Views.Dialogs.DeviceDebugView.Show(dev.name);
        }
    }
}
